package org.helgoboss.custom_one_filer_template

import org.helgoboss.project_data._
import java.io.File
import org.helgoboss.one_filer_template.OneFilerTemplate
import org.helgoboss.scala_additions.Implicits._
import org.helgoboss.basic_one_filer_template.BasicOneFilerTemplate

object CustomOneFilerTemplate extends OneFilerTemplate {
    val id = "org.helgoboss.custom"

    def extractProjectData(projectMap: Map[String, Any], sourceCode: String, sourceFile: File) = {
        val basicProjectData = BasicOneFilerTemplate.extractProjectData(projectMap, sourceCode, sourceFile)
        
        val artifactIdComponents = basicProjectData.projectId.artifactId.split("-").filterNot(_.isEmpty).toList
        lazy val defaultName = Some(artifactIdComponents.map(_.capitalize).mkString(" "))
        lazy val leafPackageNameComponents = {
            if (artifactIdComponents.last == "api") {
                artifactIdComponents.dropRight(1)
            } else {
                artifactIdComponents
            }
        }
        lazy val fullPackageName = basicProjectData.projectId.groupId + "." + leafPackageNameComponents.mkString("_")
        lazy val defaultBundleActivator = {
            val potentialBundleActivatorClassName = "Activator"
            if (sourceCode.contains("class " + potentialBundleActivatorClassName))
                Some(fullPackageName + "." + potentialBundleActivatorClassName)
            else
                None
        }
        val helgobossParentProjectId = new ProjectId {
            val groupId = "org.helgoboss"
            val artifactId = "helgoboss-parent"
            val version = "0.4"
        }
        val tmpSourceFile = sourceFile
        new ProjectData {
            val sourceCode = basicProjectData.sourceCode
            val sourceFile = Some(tmpSourceFile)
            val projectId = basicProjectData.projectId
            val name = basicProjectData.description orElse defaultName
            val description = projectMap.get("description").map(_.toString)
            val dependencies = basicProjectData.dependencies
            val bundleActivator = basicProjectData.bundleActivator orElse defaultBundleActivator
            val parentProjectId = Some(helgobossParentProjectId)
            val commandletId = basicProjectData.commandletId
            val obrRepositories = basicProjectData.obrRepositories
            val obrResources = basicProjectData.obrResources
        }
    }
}